package com.pingbu.tijian.hospital.backapi.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pingbu.common.bean.ResultBean;
import com.pingbu.common.utils.PbSecretUtils;
import com.pingbu.tijian.hospital.backapi.bean.entity.*;
import com.pingbu.tijian.hospital.backapi.mapper.EnglishWordMapper;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EnglishWordService {

    private static Logger logger = LoggerFactory.getLogger(EnglishWordService.class);

    @Autowired
    private EnglishWordMapper englishWordMapper;

    /**
     * 英文单词的分页
     * @param param
     * @return
     */
    public ResultBean<PageInfo<EnglishWord>> page(EnglishWord param){

        //先拿到一次满足searkey条件的记录
        List<EnglishWord> lst3 = englishWordMapper.list(param);
        //当前页-1*12
        int countNumb = (param.getPageNum()-1)*param.getPageSize();

        //判断搜索条件不为null lst3的长度<=当前的页数-1*12
        if(param.getSearchKey() != null && !"".equals(param.getSearchKey()) && lst3.size()<=countNumb){
            param.setPageNum(1);
        }

        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        List<EnglishWord> lst1 = englishWordMapper.list(param);
        PageInfo<EnglishWord> pi1 = new PageInfo<>(lst1);

        for(EnglishWord st: lst1){
            EnglishWord englishWord = new EnglishWord();
            englishWord.setNoid(param.getNoid());

        }

        return ResultBean.create(0,"success",pi1);
    }


    /**
     * 增加一个英文单词
     * @param param
     * @return
     */
    public ResultBean<Integer> add(EnglishWord param) {

        //
        param.setNoid(PbSecretUtils.uuid());

        EnglishWord ew = new EnglishWord();
        ew.setNoid(param.getNoid());
        ew.setEnglish_word(param.getEnglish_word());

        String englishword = ew.getEnglish_word();

        List<EnglishWord> lst = englishWordMapper.list(ew);

        for (EnglishWord ew2 : lst) {
            if (ew2.getEnglish_word().equals(englishword)) {
                return ResultBean.create(20,"单词重复，添加失败！");
            }
        }

        try {
            englishWordMapper.insert(param);
        } catch (Exception err) {
            return ResultBean.create(23, err.getMessage());
        }
        return ResultBean.create(0, "success");
    }


    /**
     * 修改某个英文单词
     * @param param
     * @return
     */
    public ResultBean<Integer> update(EnglishWord param) {
        EnglishWord ew = new EnglishWord();
        ew.setNoid(param.getNoid());
        ew.setEnglish_word(param.getEnglish_word());
        ew.setChinese(param.getChinese());
        Integer res2 = englishWordMapper.update(ew);
        return ResultBean.create(0, "success", res2);
    }

    /**
     * 删除一个英文单词
     * @param param
     * @return
     */
    public ResultBean<Integer> delete(EnglishWord param){
        Integer res = englishWordMapper.delete(param);
        return ResultBean.create(0, "success", res);
    }

}
